﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Navegador.Vistas;
using Control.Catalogo;
using Entidades.Catalogo;
using System.Net.NetworkInformation;

namespace SICOIA.UserControls.Login
{
    public partial class ucLogin : ViewController
    {
        #region "Atributos"
        private CtrlUsuario control = new CtrlUsuario();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            try
            {
                EntUsuario usuario = new EntUsuario();
                usuario.NombreUsuario = txtUsuario.Text;
                usuario.Contrasenia = txtPwd.Text;                

                usuario = control.Obtener(usuario);

                if (usuario != null)
                {
                    string MyUserName = Environment.UserName;
                    string MyDomainName = Environment.UserDomainName;
                    string MyComputerName = System.Net.Dns.GetHostName();
                    string MyIPAddress = System.Net.Dns.GetHostEntry(MyComputerName).AddressList[0].ToString();
                    string strClientOSVersion = Environment.OSVersion.ToString().Trim();

                    usuario.IpMtto = MyIPAddress;
                    usuario.PcMtto = MyComputerName;
                    usuario.UsuarioMtto = MyUserName;

                    GuardarEstado("UserPemex", usuario);
                    Response.Redirect("Marcos/control.aspx", true);
                }
                else
                    Mensaje("Usuario/Password Incorrectos");

            }
            catch (Exception ex)
            {
                Mensaje(ex.Message);
            }
        }

        private void EjecutarScript(string Script)
        {
            string msg = string.Empty;
            msg = "<script type='text/javascript' language='javascript'>";
            msg = msg + Script + "</script>";

            ScriptManager.RegisterStartupScript(UpdatePanelLogin, this.GetType(), "temp", msg, false);
        }

        private void Mensaje(string msg)
        {
            string script = string.Empty;
            string CadMensaje = string.Empty;
            CadMensaje = msg.Replace("`", "").Replace("´", "").Replace("\"", "").Replace("'`", "").Replace("'", "");
            script = "<script type='text/javascript' language='javascript'>alert('";
            script = script + CadMensaje + "');</script>";

            ScriptManager.RegisterStartupScript(UpdatePanelLogin, this.GetType(), "temp", script, false);
        }

        public string GetMACAddress()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            String sMacAddress = string.Empty;
            foreach (NetworkInterface adapter in nics)
            {
                if (sMacAddress == String.Empty)// only return MAC Address from first card  
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    sMacAddress = adapter.GetPhysicalAddress().ToString();
                }
            } return sMacAddress;
        }
    }
}